jQuery(document).ready(function() {
	//Examples of how to assign the Colorbox event to elements
	$(".group1").colorbox({rel:'group1c'});

        /* --- Menu fix top for subpage start --- */
        var distance = $('#stay_on_top').offset().top;
        jQuery(window).scroll(function(){
            var fixTopW = $('.header-container').width();
            var windowsize = jQuery(window).width();
            //$('#stay_on_top').css('width', fixTopW);
            alignMenuItems('#main_nav');
            alignMenuItems('#user_function');
            alignMenuItems('#social_tab');
            alignMenuItems('#product_tabs');
            if (windowsize > 600){
                
                if (jQuery(window).scrollTop() > distance){
                    $('#stay_on_top').addClass('fixed');
                    $('#stay_on_top').css('width', fixTopW);
                    $('.content').css('margin-top', 120);
                } else {
                    $('#stay_on_top').removeClass('fixed');
                    $('.content').css('margin-top', '0px');
                }
            }
        });
        
        jQuery(window).resize(function() {
            var fixTopW = $('.header-container').width();
            $('#stay_on_top').css('width', fixTopW);
            alignMenuItems('#main_nav');
            alignMenuItems('#user_function');
            alignMenuItems('#social_tab');
            alignMenuItems('#product_tabs');
        });
        /* --- Menu fix top for subpage end --- */
        	
        $('#PageParentId').prepend( "<option value='' selected>- All Categories</option>" );
        $('#PageLocationId').prepend( "<option value='' selected>- All Locations</option>" );
        
        
        // Carousel slider
        $("#flexiselDemo3").flexisel({
        visibleItems: 5,
        animationSpeed: 500,
        autoPlay: true,
        autoPlaySpeed: 3000,            
        pauseOnHover: true,
        clone : true,
        enableResponsiveBreakpoints: true,
        responsiveBreakpoints: { 
            portrait: { 
                changePoint:480,
                visibleItems: 2
            }, 
            landscape: { 
                changePoint:640,
                visibleItems: 4
            },
            mobileLandscape: { 
                changePoint:490,
                visibleItems: 3
            },
            tablet: { 
                changePoint:805,
                visibleItems: 3
            }
        }
    });

    $("#partner").flexisel({
        visibleItems: 5,
        animationSpeed: 500,
        autoPlay: true,
        autoPlaySpeed: 2000,            
        pauseOnHover: true,
        enableResponsiveBreakpoints: true,
        responsiveBreakpoints: { 
            portrait: { 
                changePoint:480,
                visibleItems: 2
            }, 
            landscape: { 
                changePoint:640,
                visibleItems: 4
            },
            mobileLandscape: { 
                changePoint:490,
                visibleItems: 3
            },
            tablet: { 
                changePoint:805,
                visibleItems: 3
            }
        }
    });
    
    $('.ads-partner').click(function() {
        var carouselW = $('.nbs-flexisel-container .nbs-flexisel-ul li').width();
        $('#ads_partner .nbs-flexisel-ul li').css('width', carouselW-4.8);
        $('#ads_partner .nbs-flexisel-ul').css('left', -(carouselW-4.8));
    });
    
    /* =========== Click to extend​​​​ sub category ========= */
    jQuery("#search_button").click(function(){
        jQuery("#search").slideToggle(300);
    });
    alignMenuItems('#main_nav');
    alignMenuItems('#user_function');
    alignMenuItems('#social_tab');
    alignMenuItems('#product_tabs');
    
//    jQuery("#favorite").click(
//            function()
//            {                
//                jQuery.ajax({
//                    type:'POST',
//                    async: true,
//                    cache: false,
//                    url: '<?php echo Router::Url(array('controller' => 'ajax','admin' => FALSE, 'action' => 'helloAjax'), TRUE); ?>',
//                    success: function(response) {
//                        jQuery('#resultField').val(response);
//                    },
//                    data:jQuery('form').serialize()
//                });
//                return false;
//            }
//    );
});


/* align menu */
function alignMenuItems(str){
	var totEltWidth = 0;
        if($(str).length == 0) {
            str = '#main_nav';
        }
	var menuWidth = $(''+str+'')[0].offsetWidth;
	var availableWidth = 0;
	var space = 0;

	var elts = $(''+str+' > li > a');
	elts.each(function(inx, elt) {
		// reset paddding to 0 to get correct offsetwidth
		$(elt).css('padding-left', '0px');
		$(elt).css('padding-right', '0px');

		totEltWidth += elt.offsetWidth;
	});
	availableWidth = (menuWidth - totEltWidth);
	space = Math.floor(availableWidth/(elts.length*2));
	var space_mod = availableWidth % (elts.length*2);
	elts.each(function(inx, elt) {
		if (space_mod > 0) {
			jQuery(elt).css('padding-left', space + 1 + 'px');
			space_mod = space_mod - 1;
		} else {
			jQuery(elt).css('padding-left', space + 'px');
		}
		if (space_mod > 0) {
			jQuery(elt).css('padding-right', space + 1 + 'px');
			space_mod = space_mod - 1;
		} else {
			jQuery(elt).css('padding-right', space + 'px');
		}
	});
        str = '';
}